#include "userapi.h"

UserApi::UserApi()
{
}

UserInfo UserApi::getUserInfo(QString uid)
{
    UserInfo userinfo;

    QMap<QString,QString> param;
    QString info;
    param.clear();

    param.insert("api_key", Config::APIKey);
    param.insert("format", Config::ResultFormat);
    param.insert("method", Method::USERS_GETINFO);
    param.insert("uids", uid);
    param.insert("v", Config::Version);

    QString sig = Utility::getSignature(param);
    param.insert("sig", sig);

    HttpGet http;
    info = http.post(QUrl(Config::RESTServerURL), param);

    QDomDocument doc;
    QString errorStr;
    int errorLine;
    int errorCol;
    doc.setContent(info,true, &errorStr, &errorLine, &errorCol);
    QDomElement root = doc.documentElement();
    QDomNodeList nodes = root.elementsByTagName("user");
    QDomElement element = nodes.at(0).toElement();
    userinfo.name = QString(QObject::tr(element.elementsByTagName("name").at(0).toElement().text().toUtf8().data()));
    userinfo.star = QString(QObject::tr(element.elementsByTagName("star").at(0).toElement().text().toUtf8().data()));
    userinfo.zidou = QString(QObject::tr(element.elementsByTagName("zidou").at(0).toElement().text().toUtf8().data()));
    userinfo.tinyurl = QString(QObject::tr(element.elementsByTagName("tinyurl").at(0).toElement().text().toUtf8().data()));
    userinfo.headurl = QString(QObject::tr(element.elementsByTagName("headurl").at(0).toElement().text().toUtf8().data()));

    return userinfo;
}
